package com.jerry.shop.controller;

import com.jerry.shop.converter.OrderConverter;
import com.jerry.shop.dto.OrderDto;
import com.jerry.shop.enums.ResultEnum;
import com.jerry.shop.exception.SellException;
import com.jerry.shop.form.OrderForm;
import com.jerry.shop.service.BuyerService;
import com.jerry.shop.service.OrderService;
import com.jerry.shop.utils.ResultVoUtil;
import com.jerry.shop.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单控制器
 */
@RestController
@RequestMapping("/buyer/order")
@Slf4j
public class BuyerOrderController {

    @Autowired
    private OrderService orderService;

    //创建订单
    @PostMapping("/create")
    public ResultVo<Map<String, String>> create(@Valid OrderForm orderForm,
                                                BindingResult bindingResult) {

        if (bindingResult.hasErrors()) {
            log.error("【创建订单】参数不正确, orderForm={}", orderForm);
            throw new SellException(ResultEnum.PARAM_ERROR.getCode(),
                    bindingResult.getFieldError().getDefaultMessage());
        }
        //form对象转化为dto对象
        OrderDto orderDto = OrderConverter.convert(orderForm);
        if (CollectionUtils.isEmpty(orderDto.getOrderDetailList())) {
            log.error("【创建订单】商品列表不能为空");
            throw new SellException(ResultEnum.CART_EMPTY);
        }
        //创建订单
        OrderDto createResult = orderService.create(orderDto);
        //组装返回的数据
        Map<String, String> map = new HashMap<>();
        map.put("orderId", createResult.getId());

        return ResultVoUtil.success(map);
    }

    //订单列表
    @GetMapping("/list/{id}")
    public ResultVo<List<OrderDto>> list(@PathVariable("id") String id) {

        if (StringUtils.isEmpty(id)) {
            log.error("【查询订单列表】id不能为空");
            throw new SellException(ResultEnum.PARAM_ERROR);
        }

        List<OrderDto> orderDtoList = orderService.findList(id);

        return ResultVoUtil.success(orderDtoList);
    }

    //订单列表
    @GetMapping("/listPage/{id}")
    public ResultVo<List<OrderDto>> listByPage(@PathVariable("id") String id,
                                               @RequestParam(value = "page", defaultValue = "0") Integer page,
                                               @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        if (StringUtils.isEmpty(id)) {
            log.error("【查询订单列表】id不能为空");
            throw new SellException(ResultEnum.PARAM_ERROR);
        }

        List<OrderDto> orderDtoList = orderService.findListByPage(id, page, pageSize);

        return ResultVoUtil.success(orderDtoList);
    }

    //订单详情
    @GetMapping("/detail")
    public ResultVo<OrderDto> detail(@RequestParam("buyerId") String buyerId,
                                     @RequestParam("orderId") String orderId) {
        OrderDto orderDto = orderService.findOne(buyerId, orderId);
        return ResultVoUtil.success(orderDto);
    }

    //取消订单
    @PostMapping("/cancel")
    public ResultVo cancel(@RequestParam("buyerId") String buyerId,
                           @RequestParam("orderId") String orderId) {
        orderService.cancel(buyerId, orderId);
        return ResultVoUtil.success();
    }

}
